﻿using Common;
using gregn6Lib;
using NursingFormSystem.Model;
using NursingFormSystem.NurFormsService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ShiftProject.Transfusion
{
    public partial class RecordsSafePrint : Form
    {
        private GridppReport Report = new GridppReport();
        NursingFormSystem.WebReference2.FunctionAndPermissions WebScale = new NursingFormSystem.WebReference2.FunctionAndPermissions();
        NursingFormRecord record = new NursingFormRecord();
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        DataTable dt = null;

        public RecordsSafePrint(string patientId, int VISIT_ID, int FormId)
        {
            InitializeComponent();

            var ret = formService.GetNursingFormRecord(patientId, VISIT_ID,FormId);
            if (ret.Code == 1) 
            {                        
            Transfuse tf = new Transfuse();
            NursingFormRecord record1 = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D)[0];
            tf = XmlUtil.XMLToObject<Transfuse>(record1.NursingFormData, Encoding.UTF8);
            //患者血型
            if (tf.XueXing == "")
                tf.XueXing = "555";
            else
                tf.XueXing = tf.XueXing;
            //RH
            if (tf.RH == "")
                tf.RH = "555";
            else
                tf.RH = tf.RH;
            //病人基本信息
            if (tf.HeDuiNeiRong.BingRenJiBenXinXi == "")
                tf.HeDuiNeiRong.BingRenJiBenXinXi = "555";
            else
                tf.HeDuiNeiRong.BingRenJiBenXinXi = tf.HeDuiNeiRong.BingRenJiBenXinXi;
            //血型
            if (tf.HeDuiNeiRong.XueXing == "")
                tf.HeDuiNeiRong.XueXing = "555";
            else
                tf.HeDuiNeiRong.XueXing = tf.HeDuiNeiRong.XueXing;
            //RH
            if (tf.HeDuiNeiRong.RH == "")
                tf.HeDuiNeiRong.RH = "555";
            else
                tf.HeDuiNeiRong.RH = tf.HeDuiNeiRong.RH;
            //交叉配血凝集反应
            if (tf.HeDuiNeiRong.NingJiFanYing == "")
                tf.HeDuiNeiRong.NingJiFanYing =  "555";
            else
                tf.HeDuiNeiRong.NingJiFanYing = tf.HeDuiNeiRong.NingJiFanYing;
            //血袋外观
            if (tf.HeDuiNeiRong.XueDaiWaiGuan == "")
                tf.HeDuiNeiRong.XueDaiWaiGuan = "555";
            else
                tf.HeDuiNeiRong.XueDaiWaiGuan = tf.HeDuiNeiRong.XueDaiWaiGuan;
            //血袋条形码
            if (tf.HeDuiNeiRong.XueDaiTiaoXingMa == "")
                tf.HeDuiNeiRong.XueDaiTiaoXingMa = "555";
            else
                tf.HeDuiNeiRong.XueDaiTiaoXingMa = tf.HeDuiNeiRong.XueDaiTiaoXingMa;
            //监测内容--不良反应
            if (tf.JianCeNeiRongList[2].BuLiangFanYing == "")
                tf.JianCeNeiRongList[2].BuLiangFanYing = "555";
            else
                tf.JianCeNeiRongList[2].BuLiangFanYing = tf.JianCeNeiRongList[2].BuLiangFanYing;

            if (tf.JianCeNeiRongList[3].BuLiangFanYing == "")
                tf.JianCeNeiRongList[3].BuLiangFanYing = "555";
            else
                tf.JianCeNeiRongList[3].BuLiangFanYing = tf.JianCeNeiRongList[3].BuLiangFanYing;

            if (tf.JianCeNeiRongList[4].BuLiangFanYing == "")
                tf.JianCeNeiRongList[4].BuLiangFanYing = "555";
            else
                tf.JianCeNeiRongList[4].BuLiangFanYing = tf.JianCeNeiRongList[4].BuLiangFanYing;

            if (tf.JianCeNeiRongList[5].BuLiangFanYing == "")
                tf.JianCeNeiRongList[5].BuLiangFanYing = "555";
            else
                tf.JianCeNeiRongList[5].BuLiangFanYing = tf.JianCeNeiRongList[5].BuLiangFanYing;
            //输血完将用过的血袋按规定保存24小时
            if (tf.BuLiangFanYingQingKuang.XueDaiBaoCun == "")
                tf.BuLiangFanYingQingKuang.XueDaiBaoCun = "555";
            else
                tf.BuLiangFanYingQingKuang.XueDaiBaoCun = tf.BuLiangFanYingQingKuang.XueDaiBaoCun;

            string sql = @"SELECT top 1 ( CASE (' " + tf.XueXing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS hzxx1,( CASE (' " + tf.XueXing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS hzxx2,( CASE (' " + tf.XueXing + @" ') WHEN 2 THEN 1 ELSE 0 END ) AS hzxx3,( CASE (' " + tf.XueXing + @" ') WHEN 3 THEN 1 ELSE 0 END ) AS hzxx4,
( CASE (' " + tf.RH + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS RH3,( CASE (' " + tf.RH + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS RH4,
SUBSTRING('" + tf.ShuXueRiQi + @"',1,4) AS ShuXueRiQi1,SUBSTRING('" + tf.ShuXueRiQi + @"',6,2) AS ShuXueRiQi2,SUBSTRING('" + tf.ShuXueRiQi + @"',9,2) AS ShuXueRiQi3,

( CASE (' " + tf.HeDuiNeiRong.BingRenJiBenXinXi + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS brjbxx1,( CASE (' " + tf.HeDuiNeiRong.BingRenJiBenXinXi + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS brjbxx2,
( CASE (' " + tf.HeDuiNeiRong.XueXing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS xuexi1, ( CASE (' " + tf.HeDuiNeiRong.XueXing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS xuexi2, ( CASE (' " + tf.HeDuiNeiRong.XueXing + @" ') WHEN 2 THEN 1 ELSE 0 END ) AS xuexi3, ( CASE (' " + tf.HeDuiNeiRong.XueXing + @" ') WHEN 3 THEN 1 ELSE 0 END ) AS xuexi4, 
( CASE (' " + tf.HeDuiNeiRong.RH + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS RH1, ( CASE (' " + tf.HeDuiNeiRong.RH + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS RH2,

( CASE SUBSTRING('"+tf.HeDuiNeiRong.XueYeZhongLei+@"',1,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_quanxue, ( CASE SUBSTRING('"+tf.HeDuiNeiRong.XueYeZhongLei+@"',3,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_xfhxb, ( CASE SUBSTRING('"+ tf.HeDuiNeiRong.XueYeZhongLei+ @"',5,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_xdhxb, ( CASE SUBSTRING('"+ tf.HeDuiNeiRong.XueYeZhongLei+@" ',7,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_xxb, ( CASE SUBSTRING('"+tf.HeDuiNeiRong.XueYeZhongLei+ @"',9,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_xxbdxj, ( CASE SUBSTRING('"+tf.HeDuiNeiRong.XueYeZhongLei +@"',11,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_qbdxj, ( CASE SUBSTRING('"+ tf.HeDuiNeiRong.XueYeZhongLei+@"',13,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_lcd, ( CASE SUBSTRING('"+ tf.HeDuiNeiRong.XueYeZhongLei+@"',15,1) WHEN 0 THEN 0 ELSE 1 END ) AS xyzl_qita,' " + tf.HeDuiNeiRong.XYZL_QiTa + @" ' AS xyzl_qita2,
' " + tf.HeDuiNeiRong.XueYeShuLiang + @" ' AS xysl_u,' " + tf.HeDuiNeiRong.XueYeShuLiangml + @" ' AS xysl_ml, 
( CASE (' " + tf.HeDuiNeiRong.NingJiFanYing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS jiaocha1, ( CASE (' " + tf.HeDuiNeiRong.NingJiFanYing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS jiaocha2,
( CASE (' " + tf.HeDuiNeiRong.XueDaiWaiGuan + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS xdwg1,( CASE (' " + tf.HeDuiNeiRong.XueDaiWaiGuan + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS xdwg2,
( CASE (' " + tf.HeDuiNeiRong.XueDaiTiaoXingMa + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS xdtxm1,( CASE (' " + tf.HeDuiNeiRong.XueDaiTiaoXingMa + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS xdtxm2,
' " + tf.HeDuiNeiRong.SXQHDQM_ChuHeZhe + @" ' AS sxqqm_chz,' " + tf.HeDuiNeiRong.SXQHDQM_FuHeZhe + @" ' AS sxqqm_fhz,
' " + tf.HeDuiNeiRong.CPHDQM_ChuHeZhe + @" ' AS cphdqm_chz,' " + tf.HeDuiNeiRong.CPHDQM_FuHeZhe + @" ' AS cphdqm_fhz,

' " + tf.JianCeNeiRongList[0].ShiJian + @" ' AS shijian1,' " + tf.JianCeNeiRongList[1].ShiJian + @" ' AS shijian2,' " + tf.JianCeNeiRongList[2].ShiJian + @" ' AS shijian3,' " + tf.JianCeNeiRongList[3].ShiJian + @" ' AS shijian4,' " + tf.JianCeNeiRongList[4].ShiJian + @" ' AS shijian5,' " + tf.JianCeNeiRongList[5].ShiJian + @" ' AS shijian6,
' " + tf.JianCeNeiRongList[0].ShenZhi + @" ' AS shenzhi1,' " + tf.JianCeNeiRongList[1].ShenZhi + @" ' AS shenzhi2,' " + tf.JianCeNeiRongList[2].ShenZhi + @" ' AS shenzhi3,' " + tf.JianCeNeiRongList[3].ShenZhi + @" ' AS shenzhi4,' " + tf.JianCeNeiRongList[4].ShenZhi + @" ' AS shenzhi5,' " + tf.JianCeNeiRongList[5].ShenZhi + @" ' AS shenzhi6,
' " + tf.JianCeNeiRongList[0].TiWen + @" ' AS tiwen1,' " + tf.JianCeNeiRongList[1].TiWen + @" ' AS tiwen2,' " + tf.JianCeNeiRongList[2].TiWen + @" ' AS tiwen3,' " + tf.JianCeNeiRongList[3].TiWen + @" ' AS tiwen4,' " + tf.JianCeNeiRongList[4].TiWen + @" ' AS tiwen5,' " + tf.JianCeNeiRongList[5].TiWen + @" ' AS tiwen6,
' " + tf.JianCeNeiRongList[0].MaiBo + @" ' AS maibo1,' " + tf.JianCeNeiRongList[1].MaiBo + @" ' AS maibo2,' " + tf.JianCeNeiRongList[2].MaiBo + @" ' AS maibo3,' " + tf.JianCeNeiRongList[3].MaiBo + @" ' AS maibo4,' " + tf.JianCeNeiRongList[4].MaiBo + @" ' AS maibo5,' " + tf.JianCeNeiRongList[5].MaiBo + @" ' AS maibo6,
' " + tf.JianCeNeiRongList[0].HuXi + @" ' AS huxi1,' " + tf.JianCeNeiRongList[1].HuXi + @" ' AS huxi2,' " + tf.JianCeNeiRongList[2].HuXi + @" ' AS huxi3,' " + tf.JianCeNeiRongList[3].HuXi + @" ' AS huxi4,' " + tf.JianCeNeiRongList[4].HuXi + @" ' AS huxi5,' " + tf.JianCeNeiRongList[5].HuXi + @" ' AS huxi6,
' " + tf.JianCeNeiRongList[0].XueYa + @" ' AS xueya1,' " + tf.JianCeNeiRongList[1].XueYa + @" ' AS xueya2,' " + tf.JianCeNeiRongList[2].XueYa + @" ' AS xueya3,' " + tf.JianCeNeiRongList[3].XueYa + @" ' AS xueya4,' " + tf.JianCeNeiRongList[4].XueYa + @" ' AS xueya5,' " + tf.JianCeNeiRongList[5].XueYa + @" ' AS xueya6,
' " + tf.JianCeNeiRongList[0].SuDu + @" ' AS sudu1,' " + tf.JianCeNeiRongList[1].SuDu + @" ' AS sudu2,' " + tf.JianCeNeiRongList[2].SuDu + @" ' AS sudu3,' " + tf.JianCeNeiRongList[3].SuDu + @" ' AS sudu4,' " + tf.JianCeNeiRongList[4].SuDu + @" ' AS sudu5,' " + tf.JianCeNeiRongList[5].SuDu + @" ' AS sudu6,
' " + tf.JianCeNeiRongList[0].BuLiangFanYing + @" ' AS blfy1,' " + tf.JianCeNeiRongList[1].BuLiangFanYing + @" ' AS blfy2, ( CASE (' " + tf.JianCeNeiRongList[2].BuLiangFanYing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS blfy3, ( CASE (' " + tf.JianCeNeiRongList[2].BuLiangFanYing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS blfy3_1, ( CASE (' " + tf.JianCeNeiRongList[3].BuLiangFanYing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS blfy4, ( CASE (' " + tf.JianCeNeiRongList[3].BuLiangFanYing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS blfy4_1, ( CASE (' " + tf.JianCeNeiRongList[4].BuLiangFanYing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS blfy5, ( CASE (' " + tf.JianCeNeiRongList[4].BuLiangFanYing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS blfy5_1, ( CASE (' " + tf.JianCeNeiRongList[5].BuLiangFanYing + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS blfy6, ( CASE (' " + tf.JianCeNeiRongList[5].BuLiangFanYing + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS blfy6_1,
' " + tf.JianCeNeiRongList[0].HuShiQianMing + @" ' AS hsqm1,' " + tf.JianCeNeiRongList[1].HuShiQianMing + @" ' AS hsqm2,' " + tf.JianCeNeiRongList[2].HuShiQianMing + @" ' AS hsqm3,' " + tf.JianCeNeiRongList[3].HuShiQianMing + @" ' AS hsqm4,' " + tf.JianCeNeiRongList[4].HuShiQianMing + @" ' AS hsqm5,' " + tf.JianCeNeiRongList[5].HuShiQianMing + @" ' AS hsqm6,

' " + tf.BuLiangFanYingQingKuang.BLFYQK_NeiRong + @" ' AS  memoEditbuliangfanyin,

( CASE (' " + tf.BuLiangFanYingQingKuang.XueDaiBaoCun + @" ') WHEN 0 THEN 1 ELSE 0 END ) AS xdbu1, ( CASE (' " + tf.BuLiangFanYingQingKuang.XueDaiBaoCun + @" ') WHEN 1 THEN 1 ELSE 0 END ) AS xdbu2,

PATIENT_NAME,SEX,AGE,NURSE_CELL_CODE,NURSE_CELL_NAME,DEPARTMENT_NAME,BED_NO,PATIENT_ID 
FROM NursingFormRecord LEFT JOIN dbo.V_YDHL_PATIENT
ON NursingFormRecord.patientId = V_YDHL_PATIENT.patient_id WHERE PatientId='" + patientId + "' and formid='" + FormId + "' ";

            axGRPrintViewer1.ShowPrintDlg = false;//设置 ShowPrintDlg 属性，让点击工具栏中的打印按钮时不显示打印对话框
            try
            {
                if (File.Exists(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\安全输血护理.grf"))
                {
                    Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\安全输血护理.grf");
                }
                else
                {
                    Report.LoadFromFile(@"Report.LoadFromFile(../../Ydhl_GRF/安全输血护理.grf");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("加载临时医嘱表单失败，请检查文件Ydhl_GRF/安全输血护理.grf是否存在");
                WriteLog.WriteLogString(ex.Message + ".  加载血糖监测记录表单失败，请检查文件Ydhl_GRF/安全输血护理.grf是否存在");
            }

            Report.DetailGrid.Recordset.QuerySQL = sql;

            axGRPrintViewer1.Report = Report;
            axGRPrintViewer1.Start();
            }
            else if (ret.Code == 2)
            {
                string sql = @"SELECT top 1
PATIENT_NAME,SEX,AGE,NURSE_CELL_CODE,DEPARTMENT_NAME,BED_NO,PATIENT_ID,NURSE_CELL_NAME,
'' AS hzxx1,'' AS hzxx2,'' AS hzxx3,'' AS hzxx4,
'' AS RH3,'' AS RH4,
'' AS ShuXueRiQi1,'' AS ShuXueRiQi2,'' AS ShuXueRiQi3,

'' AS brjbxx1,'' AS brjbxx2,
'' AS xuexi1, '' AS xuexi2, '' AS xuexi3,'' AS xuexi4, 
'' AS RH1, '' AS RH2,

'' AS xyzl_quanxue, '' AS xyzl_xfhxb, '' AS xyzl_xdhxb, '' AS xyzl_xxb, '' AS xyzl_xxbdxj, '' AS xyzl_qbdxj, '' AS xyzl_lcd, '' AS xyzl_qita,'  ' AS xyzl_qita2,
'' AS xysl_u,'' AS xysl_ml, 
'' AS jiaocha1, '' AS jiaocha2,
'' AS xdwg1,'' AS xdwg2,
'' AS xdtxm1,'' AS xdtxm2,
'' AS sxqqm_chz,'' AS sxqqm_fhz,
'' AS cphdqm_chz,'' AS cphdqm_fhz,

'' AS shijian1,'' AS shijian2,'' AS shijian3,'' AS shijian4,'' AS shijian5,'' AS shijian6,
'' AS shenzhi1,'' AS shenzhi2,'' AS shenzhi3,'' AS shenzhi4,'' AS shenzhi5,'' AS shenzhi6,
'' AS tiwen1,'' AS tiwen2,'' AS tiwen3,'' AS tiwen4,'' AS tiwen5,'' AS tiwen6,
'' AS maibo1,'' AS maibo2,'' AS maibo3,'' AS maibo4,'' AS maibo5,'' AS maibo6,
'' AS huxi1,'' AS huxi2,'' AS huxi3,'' AS huxi4,'' AS huxi5,'' AS huxi6,
'' AS xueya1,'' AS xueya2,'' AS xueya3,'' AS xueya4,'' AS xueya5,'' AS xueya6,
' ／ ' AS sudu1,'' AS sudu2,'' AS sudu3,'' AS sudu4,'' AS sudu5,' ／ ' AS sudu6,
' ／ ' AS blfy1,'／ ' AS blfy2, '' AS blfy3, '' AS blfy3_1, '' AS blfy4, '' AS blfy4_1, '' AS blfy5, '' AS blfy5_1, '' AS blfy6, '' AS blfy6_1,
'' AS hsqm1,'' AS hsqm2,'' AS hsqm3,'' AS hsqm4,'' AS hsqm5,'' AS hsqm6,

'' AS memoEditbuliangfanyin,

'' AS xdbu1, '' AS xdbu2
FROM dbo.V_YDHL_PATIENT
 WHERE Patient_Id='" + patientId + "' ";

                axGRPrintViewer1.ShowPrintDlg = false;//设置 ShowPrintDlg 属性，让点击工具栏中的打印按钮时不显示打印对话框
                try
                {
                    if (File.Exists(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\安全输血护理.grf"))
                    {
                        Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\安全输血护理.grf");
                    }
                    else
                    {
                        Report.LoadFromFile(@"Report.LoadFromFile(../../Ydhl_GRF/安全输血护理.grf");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("加载临时医嘱表单失败，请检查文件Ydhl_GRF/安全输血护理.grf是否存在");
                    WriteLog.WriteLogString(ex.Message + ".  加载血糖监测记录表单失败，请检查文件Ydhl_GRF/安全输血护理.grf是否存在");
                }
                
                Report.DetailGrid.Recordset.QuerySQL = sql;

                axGRPrintViewer1.Report = Report;
                axGRPrintViewer1.Start();
            }
        }
     
    }
}
